Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 506)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.90270 12.90563 12.90857 12.91149 12.91439 12.91727 12.92010 12.92289
##   [9] 12.92562 12.92828 12.93086 12.93336 12.93577 12.93807 12.94025 12.94231
##  [17] 12.94424 12.94603 12.94767 12.94914 12.95044 12.95157 12.95254 12.95338
##  [25] 12.95410 12.95471 12.95521 12.95561 12.95591 12.95611 12.95623 12.95626
##  [33] 12.95621 12.95609 12.95591 12.95566 12.95535 12.95499 12.95458 12.95414
##  [41] 12.95365 12.95313 12.95259 12.95202 12.95144 12.95085 12.95025 12.94965
##  [49] 12.94906 12.94847 12.94781 12.94699 12.94601 12.94490 12.94366 12.94230
##  [57] 12.94083 12.93926 12.93760 12.93585 12.93404 12.93217 12.93024 12.92827
##  [65] 12.92628 12.92426 12.92223 12.92019 12.91817 12.91616 12.91418 12.91224
##  [73] 12.91035 12.90851 12.90674 12.90505 12.90344 12.90194 12.90001 12.89718
##  [81] 12.89351 12.88909 12.88396 12.87820 12.87188 12.86506 12.85781 12.85020
##  [89] 12.84229 12.83416 12.82586 12.81747 12.80906 12.80069 12.79243 12.78434
##  [97] 12.77649 12.76896 12.76181 12.75509 12.74890 12.74328 12.73831 12.73405
## [105] 12.73057 12.72795 12.72555 12.72275 12.71961 12.71617 12.71250 12.70864
## [113] 12.70466 12.70060 12.69652 12.69248 12.68853 12.68471 12.68110 12.67774
## [121] 12.67468 12.67198 12.66970 12.66788 12.66659 12.66587 12.66579 12.66639
## [129] 12.66773 12.66987 12.67285 12.67674 12.68158 12.68812 12.69684 12.70744
## [137] 12.71962 12.73306 12.74745 12.76250 12.77788 12.79329 12.80843 12.82298
## [145] 12.83664 12.84910 12.86005 12.87092 12.88324 12.89689 12.91171 12.92757
## [153] 12.94433 12.96185 12.97998 12.99859 13.01754 13.03668 13.05588 13.07500
## [161] 13.09389 13.11241 13.13043 13.14780 13.16439 13.18006 13.19465 13.20804
## [169] 13.22009 13.23064 13.24131 13.25365 13.26748 13.28261 13.29884 13.31600
## [177] 13.33389 13.35232 13.37110 13.39005 13.40898 13.42770 13.44601 13.46374
## [185] 13.48069 13.49667 13.51150 13.52498 13.53693 13.54716 13.55547 13.56169
## [193] 13.56562 13.56708 13.56586 13.56180 13.55496 13.54573 13.53438 13.52118
## [201] 13.50639 13.49028 13.47311 13.45516 13.43670 13.41798 13.39928 13.38087
## [209] 13.36301 13.34596 13.32752 13.30559 13.28065 13.25317 13.22362 13.19249
## [217] 13.16024 13.12735 13.09429 13.06154 13.02956 12.99884 12.96984 12.94305
## [225] 12.91459 12.88087 12.84275 12.80110 12.75679 12.71068 12.66365 12.61657
## [233] 12.57029 12.52568 12.48363 12.44498 12.41061 12.38139 12.35383 12.32418
## [241] 12.29289 12.26043 12.22725 12.19382 12.16060 12.12805 12.09663 12.06681
## [249] 12.03903 12.01376 11.99148 11.97262 11.95688 11.94344 11.93206 11.92248
## [257] 11.91442 11.90764 11.90188 11.89688 11.89238 11.88812 11.88384 11.87929
## [265] 11.87420 11.86832 11.86327 11.86069 11.86036 11.86203 11.86549 11.87051
## [273] 11.87685 11.88430 11.89260 11.90155 11.91091 11.92045 11.92995 11.93916
## [281] 11.94788 11.95585 11.96287 11.96869 11.97309 11.97584 11.97671 11.97547
## [289] 11.97190 11.96800 11.96574 11.96481 11.96489 11.96569 11.96689 11.96818
## [297] 11.96926 11.96981 11.96954 11.96812 11.96525 11.96063 11.95394 11.94457
## [305] 11.93238 11.91778 11.90117 11.88294 11.86349 11.84323 11.82256 11.80188
## [313] 11.78158 11.76207 11.74375 11.72702 11.71227 11.69619 11.67566 11.65146
## [321] 11.62436 11.59510 11.56447 11.53322 11.50211 11.47192 11.44340 11.41733
## [329] 11.39446 11.37555 11.36138 11.34847 11.33320 11.31610 11.29772 11.27858
## [337] 11.25924 11.24022 11.22206 11.20530 11.19048 11.17814 11.16881 11.16303
## [345] 11.16133 11.16297 11.16671 11.17237 11.17978 11.18876 11.19912 11.21069
## [353] 11.22330 11.23676 11.25089 11.26552 11.28047 11.29556 11.31061 11.32701
## [361] 11.34610 11.36766 11.39142 11.41715 11.44460 11.47353 11.50370 11.53486
## [369] 11.56677 11.59919 11.63187 11.66457 11.69705 11.72907 11.76037 11.79072
## [377] 11.81987 11.84759 11.87611 11.90766 11.94193 11.97862 12.01742 12.05804
## [385] 12.10018 12.14352 12.18777 12.23263 12.27779 12.32295 12.36781 12.41207
## [393] 12.45542 12.49757 12.53820 12.57703 12.61374 12.64804 12.67962 12.70818
## [401] 12.73341 12.75750 12.78258 12.80833 12.83442 12.86051 12.88627 12.91137
## [409] 12.93548 12.95827 12.97939 12.99853 13.01535 13.02951 13.04069 13.04908
## [417] 13.05522 13.05931 13.06159 13.06224 13.06148 13.05952 13.05656 13.05283
## [425] 13.04851 13.04384 13.03900 13.03422 13.02971 13.02397 13.01551 13.00455
## [433] 12.99127 12.97587 12.95855 12.93951 12.91895 12.89706 12.87405 12.85012
## [441] 12.82546 12.80027 12.77475 12.74909 12.72351 12.69819 12.67333 12.64913
## [449] 12.62580 12.60353 12.58251 12.56295 12.54504 12.52899 12.51499 12.50158
## [457] 12.48730 12.47234 12.45689 12.44116 12.42533 12.40960 12.39417 12.37924
## [465] 12.36499 12.35162 12.33934 12.32832 12.31878 12.30997 12.30108 12.29219
## [473] 12.28338 12.27473 12.26631 12.25821 12.25049 12.24323 12.23652 12.23043
## [481] 12.22504 12.22043 12.21666 12.21362 12.21110 12.20910 12.20762 12.20666
## [489] 12.20621 12.20626 12.20681 12.20787 12.20942 12.21145 12.21398 12.21698
## [497] 12.22046 12.22442 12.22885 12.23374 12.23910 12.24491 12.25117 12.25789
## [505] 12.26505 12.27265
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 506)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.56011 12.55884 12.55762 12.55646 12.55536 12.55431 12.55331 12.55235
##   [9] 12.55144 12.55056 12.54972 12.54890 12.54812 12.54735 12.54661 12.54588
##  [17] 12.54517 12.54446 12.54376 12.54307 12.54237 12.54166 12.54095 12.54024
##  [25] 12.53953 12.53882 12.53811 12.53742 12.53674 12.53606 12.53541 12.53477
##  [33] 12.53415 12.53355 12.53298 12.53243 12.53191 12.53143 12.53098 12.53056
##  [41] 12.53018 12.52985 12.52956 12.52931 12.52911 12.52897 12.52887 12.52883
##  [49] 12.52885 12.52893 12.52899 12.52895 12.52883 12.52865 12.52840 12.52810
##  [57] 12.52777 12.52741 12.52704 12.52667 12.52631 12.52597 12.52566 12.52540
##  [65] 12.52519 12.52505 12.52500 12.52503 12.52517 12.52542 12.52579 12.52631
##  [73] 12.52697 12.52780 12.52879 12.52997 12.53135 12.53293 12.53456 12.53608
##  [81] 12.53750 12.53883 12.54009 12.54128 12.54243 12.54354 12.54463 12.54570
##  [89] 12.54678 12.54787 12.54898 12.55014 12.55135 12.55262 12.55397 12.55540
##  [97] 12.55694 12.55860 12.56038 12.56230 12.56437 12.56661 12.56903 12.57163
## [105] 12.57444 12.57747 12.57984 12.58079 12.58048 12.57906 12.57667 12.57348
## [113] 12.56964 12.56529 12.56061 12.55573 12.55081 12.54601 12.54148 12.53738
## [121] 12.53385 12.53105 12.52913 12.52825 12.52857 12.53023 12.53338 12.53819
## [129] 12.54511 12.55429 12.56541 12.57816 12.59224 12.60733 12.62312 12.63930
## [137] 12.65556 12.67159 12.68707 12.70170 12.71516 12.72715 12.73952 12.75422
## [145] 12.77105 12.78982 12.81034 12.83241 12.85584 12.88043 12.90600 12.93234
## [153] 12.95927 12.98659 13.01410 13.04162 13.06894 13.09588 13.12224 13.14783
## [161] 13.17245 13.19592 13.21803 13.23859 13.25741 13.27429 13.28905 13.30148
## [169] 13.31348 13.32689 13.34149 13.35707 13.37340 13.39026 13.40743 13.42468
## [177] 13.44180 13.45855 13.47472 13.49009 13.50443 13.51752 13.52913 13.53906
## [185] 13.54706 13.55293 13.55644 13.55736 13.55548 13.55079 13.54361 13.53417
## [193] 13.52272 13.50948 13.49469 13.47859 13.46142 13.44342 13.42481 13.40584
## [201] 13.38675 13.36776 13.34912 13.33107 13.31384 13.29508 13.27264 13.24705
## [209] 13.21885 13.18855 13.15670 13.12383 13.09046 13.05712 13.02435 12.99268
## [217] 12.96263 12.93474 12.90954 12.88282 12.85061 12.81383 12.77339 12.73020
## [225] 12.68518 12.63923 12.59328 12.54824 12.50501 12.46451 12.42766 12.39536
## [233] 12.36853 12.34337 12.31580 12.28634 12.25549 12.22378 12.19170 12.15977
## [241] 12.12850 12.09840 12.06998 12.04375 12.02023 11.99992 11.98334 11.96968
## [249] 11.95772 11.94731 11.93830 11.93055 11.92392 11.91826 11.91343 11.90928
## [257] 11.90567 11.90246 11.89950 11.89665 11.89376 11.89244 11.89424 11.89890
## [265] 11.90618 11.91582 11.92758 11.94121 11.95646 11.97309 11.99084 12.00946
## [273] 12.02871 12.04833 12.06809 12.08772 12.10699 12.12564 12.14343 12.16009
## [281] 12.17540 12.18910 12.20093 12.21066 12.21802 12.22278 12.22469 12.22583
## [289] 12.22823 12.23158 12.23556 12.23985 12.24413 12.24807 12.25136 12.25367
## [297] 12.25469 12.25410 12.25156 12.24678 12.23942 12.22809 12.21210 12.19214
## [305] 12.16894 12.14320 12.11562 12.08692 12.05781 12.02898 12.00116 11.97504
## [313] 11.95135 11.93078 11.91404 11.89709 11.87588 11.85113 11.82356 11.79391
## [321] 11.76288 11.73121 11.69962 11.66882 11.63955 11.61252 11.58846 11.56810
## [329] 11.55214 11.53746 11.52073 11.50241 11.48295 11.46280 11.44244 11.42230
## [337] 11.40284 11.38453 11.36781 11.35314 11.34098 11.33179 11.32601 11.32204
## [345] 11.31809 11.31431 11.31087 11.30793 11.30565 11.30419 11.30372 11.30440
## [353] 11.30638 11.30984 11.31494 11.32183 11.33069 11.34245 11.35770 11.37602
## [361] 11.39700 11.42025 11.44535 11.47189 11.49946 11.52767 11.55610 11.58434
## [369] 11.61199 11.63864 11.66389 11.68731 11.71096 11.73701 11.76525 11.79550
## [377] 11.82756 11.86124 11.89634 11.93267 11.97004 12.00824 12.04708 12.08638
## [385] 12.12593 12.16554 12.20501 12.24416 12.28279 12.32069 12.35768 12.39357
## [393] 12.42815 12.46124 12.49264 12.52215 12.54958 12.57473 12.59742 12.61986
## [401] 12.64412 12.66982 12.69655 12.72392 12.75152 12.77896 12.80583 12.83175
## [409] 12.85631 12.87912 12.89977 12.91786 12.93300 12.94575 12.95699 12.96683
## [417] 12.97537 12.98271 12.98895 12.99421 12.99857 13.00215 13.00504 13.00734
## [425] 13.00916 13.01061 13.01178 13.01176 13.00973 13.00590 13.00047 12.99365
## [433] 12.98567 12.97673 12.96704 12.95681 12.94625 12.93558 12.92501 12.91475
## [441] 12.90500 12.89461 12.88239 12.86858 12.85341 12.83711 12.81991 12.80202
## [449] 12.78369 12.76514 12.74660 12.72830 12.71046 12.69332 12.67709 12.66084
## [457] 12.64354 12.62533 12.60633 12.58667 12.56649 12.54591 12.52507 12.50408
## [465] 12.48309 12.46223 12.44162 12.42139 12.40167 12.38204 12.36199 12.34157
## [473] 12.32079 12.29970 12.27833 12.25670 12.23484 12.21280 12.19060 12.16827
## [481] 12.14584 12.12335 12.10083 12.07823 12.05548 12.03259 12.00955 11.98636
## [489] 11.96302 11.93951 11.91585 11.89202 11.86802 11.84386 11.81952 11.79501
## [497] 11.77032 11.74545 11.72040 11.69515 11.66972 11.64410 11.61828 11.59227
## [505] 11.56605 11.53963
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 506)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.78424 11.79148 11.79871 11.80593 11.81312 11.82026 11.82734 11.83435
##   [9] 11.84126 11.84806 11.85475 11.86130 11.86769 11.87392 11.87996 11.88580
##  [17] 11.89144 11.89684 11.90200 11.90690 11.91152 11.91586 11.91989 11.92360
##  [25] 11.92698 11.93001 11.93267 11.93494 11.93683 11.93835 11.93955 11.94045
##  [33] 11.94107 11.94142 11.94152 11.94139 11.94103 11.94047 11.93973 11.93880
##  [41] 11.93773 11.93651 11.93516 11.93371 11.93216 11.93053 11.92884 11.92710
##  [49] 11.92534 11.92356 11.92177 11.92001 11.91828 11.91659 11.91497 11.91343
##  [57] 11.91198 11.91014 11.90743 11.90392 11.89967 11.89475 11.88922 11.88314
##  [65] 11.87657 11.86958 11.86223 11.85458 11.84670 11.83865 11.83049 11.82228
##  [73] 11.81410 11.80599 11.79803 11.79027 11.78279 11.77563 11.76888 11.76258
##  [81] 11.75680 11.75161 11.74706 11.74323 11.74016 11.73655 11.73112 11.72403
##  [89] 11.71544 11.70550 11.69438 11.68221 11.66918 11.65542 11.64110 11.62637
##  [97] 11.61139 11.59631 11.58130 11.56651 11.55209 11.53820 11.52500 11.51265
## [105] 11.50129 11.49109 11.48221 11.47479 11.46901 11.46500 11.46294 11.46297
## [113] 11.46525 11.46976 11.47625 11.48458 11.49459 11.50613 11.51904 11.53319
## [121] 11.54840 11.56454 11.58145 11.59897 11.61696 11.63527 11.65373 11.67220
## [129] 11.69052 11.70855 11.72613 11.74311 11.75933 11.77774 11.80087 11.82801
## [137] 11.85843 11.89140 11.92620 11.96211 11.99841 12.03436 12.06924 12.10233
## [145] 12.13290 12.16024 12.18360 12.20578 12.22987 12.25568 12.28300 12.31163
## [153] 12.34137 12.37200 12.40332 12.43514 12.46723 12.49941 12.53146 12.56318
## [161] 12.59437 12.62482 12.65432 12.68267 12.70967 12.73512 12.75880 12.78051
## [169] 12.80005 12.81721 12.83330 12.84968 12.86628 12.88299 12.89973 12.91641
## [177] 12.93293 12.94922 12.96517 12.98071 12.99573 13.01015 13.02387 13.03682
## [185] 13.04890 13.06001 13.07007 13.07900 13.08668 13.09305 13.09801 13.10147
## [193] 13.10333 13.10351 13.10193 13.09848 13.09354 13.08756 13.08054 13.07247
## [201] 13.06336 13.05321 13.04202 13.02979 13.01653 13.00223 12.98690 12.97053
## [209] 12.95314 12.93471 12.91302 12.88629 12.85528 12.82072 12.78333 12.74387
## [217] 12.70306 12.66164 12.62035 12.57993 12.54111 12.50462 12.47121 12.44161
## [225] 12.41068 12.37348 12.33112 12.28465 12.23518 12.18377 12.13150 12.07946
## [233] 12.02872 11.98038 11.93550 11.89516 11.86046 11.83246 11.80652 11.77767
## [241] 11.74651 11.71365 11.67969 11.64524 11.61090 11.57726 11.54495 11.51455
## [249] 11.48667 11.46192 11.44089 11.42420 11.41188 11.40323 11.39776 11.39499
## [257] 11.39444 11.39561 11.39802 11.40117 11.40459 11.40779 11.41028 11.41156
## [265] 11.41116 11.40859 11.40699 11.40960 11.41600 11.42580 11.43861 11.45402
## [273] 11.47163 11.49106 11.51189 11.53373 11.55619 11.57885 11.60134 11.62324
## [281] 11.64416 11.66371 11.68147 11.69706 11.71008 11.72012 11.72680 11.72971
## [289] 11.72845 11.72383 11.71710 11.70850 11.69829 11.68669 11.67397 11.66036
## [297] 11.64611 11.63147 11.61669 11.60200 11.58766 11.57391 11.56100 11.54438
## [305] 11.52015 11.48957 11.45388 11.41432 11.37216 11.32862 11.28495 11.24241
## [313] 11.20223 11.16566 11.13395 11.10835 11.09010 11.07513 11.05879 11.04141
## [321] 11.02332 11.00488 10.98643 10.96829 10.95082 10.93435 10.91922 10.90577
## [329] 10.89436 10.88530 10.87895 10.87480 10.87208 10.87067 10.87049 10.87145
## [337] 10.87343 10.87635 10.88011 10.88462 10.88977 10.89547 10.90163 10.90815
## [345] 10.91493 10.92312 10.93374 10.94650 10.96113 10.97734 10.99485 11.01337
## [353] 11.03262 11.05232 11.07219 11.09195 11.11130 11.12998 11.14769 11.16596
## [361] 11.18638 11.20873 11.23278 11.25834 11.28518 11.31308 11.34184 11.37124
## [369] 11.40105 11.43108 11.46110 11.49089 11.52024 11.54895 11.57678 11.60353
## [377] 11.62898 11.65292 11.67660 11.70134 11.72702 11.75352 11.78073 11.80854
## [385] 11.83681 11.86545 11.89432 11.92331 11.95230 11.98119 12.00984 12.03814
## [393] 12.06598 12.09324 12.11980 12.14554 12.17035 12.19411 12.21670 12.23801
## [401] 12.25791 12.27821 12.30050 12.32435 12.34933 12.37502 12.40097 12.42677
## [409] 12.45199 12.47618 12.49894 12.51981 12.53838 12.55422 12.56689 12.57711
## [417] 12.58592 12.59343 12.59974 12.60493 12.60911 12.61237 12.61480 12.61651
## [425] 12.61759 12.61814 12.61825 12.61802 12.61754 12.61599 12.61254 12.60733
## [433] 12.60049 12.59214 12.58241 12.57142 12.55932 12.54622 12.53225 12.51755
## [441] 12.50223 12.48643 12.47027 12.45389 12.43741 12.42095 12.40466 12.38864
## [449] 12.37305 12.35799 12.34360 12.33000 12.31733 12.30572 12.29528 12.28521
## [457] 12.27464 12.26367 12.25236 12.24080 12.22906 12.21722 12.20536 12.19355
## [465] 12.18189 12.17043 12.15927 12.14848 12.13813 12.12790 12.11742 12.10675
## [473] 12.09591 12.08497 12.07396 12.06294 12.05193 12.04100 12.03018 12.01953
## [481] 12.00907 11.99887 11.98896 11.97927 11.96968 11.96020 11.95082 11.94154
## [489] 11.93236 11.92327 11.91428 11.90538 11.89656 11.88783 11.87919 11.87062
## [497] 11.86214 11.85373 11.84540 11.83714 11.82896 11.82084 11.81278 11.80479
## [505] 11.79686 11.78899
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")